<?php
namespace App\Admin\Controllers\FileModel;
use App\Http\Controllers\Controller;
use Dcat\Admin\FormStep\Form as StepForm;
use App\Models\InputStepForm;
use App\Models\InformationList;
use Dcat\Admin\Form;
use Dcat\Admin\Admin;
use Dcat\Admin\Layout\Content;
use Illuminate\Http\Request;
use PhpOffice\PhpWord\TemplateProcessor;
use PhpOffice\PhpWord\PhpWord;
use Dcat\Admin\Grid;
use Illuminate\Support\Facades\DB;
use Log;
// use Dcat\Admin\Widgets\Form;
class EngineeringCommissioningController extends Controller
{
    //智能变频脉冲电源安装调试完成确认单
    protected $options = [
        1 => '显示文本框',
        2 => '显示编辑器',
        3 => '显示文件上传',
        4 => '还是显示文本框',
    ];
    // use PreviewCode;
    public function index(Content $content)
    {
        return $content
            // ->body('<div style="margin:5px 0 15px;">'.$this->buildPreviewButton().'</div>')
            ->body($this->form())
            ->header('智能变频脉冲电源安装调试完成确认单')
            ->description('分步表单');
    }
    protected function form()
{
    Admin::js('/stepform/js/jquery.smartWizard.min.js');
    Admin::css('/stepform/css/step.css');
    return Form::make(new InputStepForm(), function (Form $form) {
            $form->title('智能变频脉冲电源安装调试完成确认单');
            $form->action('EngineeringCommissioningstep');
            $form->disableListButton();
            $form->multipleSteps()
                ->remember() 
                ->width('100%')
                ->add('基本信息', function (StepForm $step) use ($form) {
                    $step->text('pa1',trans('用户名称'))->required();
                    $step->text('pa2',trans('用户电话'))->required();
                    $step->text('pa3',trans('联系部门/人'))->required();
                    $step->text('pa4',trans('用户传真'))->required();
                    $step->text('pa5',trans('用户地址'))->required();
                    $step->text('pa6',trans('用户邮编'))->required();
                    $step->text('pa7',trans('本体制造厂'))->required();
                    $step->text('pa8',trans('使用场合'))->required();
                    $step->text('pa9',trans('本体型号规格'))->required();
                    $step->text('pa10',trans('电 场'))->required();
                    $step->text('pa11',trans('高压整流设备制造厂'))->required();
                    $step->text('pa12',trans('低压控制（含端子箱）型号规格\数量'))->required();
                    $step->text('pa13',trans('高压整流设备型号规格\数量'))->required();
                    // $step->text('pa14',trans(''))->required();
                    $step->text('pa15',trans('设备调试情况'))->required();
                    $step->text('pa16',trans('设备调试结论'))->required();
                    $step->text('pa17',trans('备注'))->required();
                    $step->datetime('pa18',trans('调试时间'))->required();
                    $step->text('pa19',trans('调试人员'))->required();
                    $step->table('list', trans('高压柜设置数据'),function ($table) {
                        $table->text('出厂编号')->width(100)->required();
                        $table->text('电场名称')->required();
                        $table->text('一次电压百分比')->required();
                        $table->text('一次电流百分比')->required();
                        $table->text('二次电压百分比')->required();
                        $table->text('二次电流百分比')->required();
                        $table->text('IP地址')->required();
                        $table->text('变压器型号')->required();
                        $table->text('变压器抽头')->required();
                    })->width(12)->rules('required');
                    $step->table('list1', trans('运行参数记录表'),function ($table) {
                        $table->text('输出比')->required();
                        $table->text('输出频率')->required();
                        $table->text('一次电压')->required();
                        $table->text('一次电流')->required();
                        $table->text('二次电压')->required();
                        $table->text('二次电压取样电阻')->required();
                        $table->text('二次电压反馈')->required();
                        $table->text('二次电流')->required();
                        $table->text('二次电流取样电阻')->required();
                    })->width(12)->rules('required');
                });
        });
    }

    public function step(Request $request){
        $info = $request->all();
        if(empty($info['CURRENT_VALIDATION_STEP'])&&$info['ALL_STEPS']==1){//提交
            Log::info($info);
            //实例化 phpword 类
            $PHPWord = new PhpWord();
            //指定事先制作好的模板文件路径
            $templateProcessor = new TemplateProcessor(public_path("filemodel/engineering_commissioning.docx"));
            // $templateProcessor->setValue('projectname', $info['projectname']);
            $templateProcessor->setValue('pa1', $info['pa1']);
            $templateProcessor->setValue('pa2', $info['pa2']);
            $templateProcessor->setValue('pa3', $info['pa3']);
            $templateProcessor->setValue('pa4', $info['pa4']);
            $templateProcessor->setValue('pa5', $info['pa5']);
            $templateProcessor->setValue('pa6', $info['pa6']);
            $templateProcessor->setValue('pa7', $info['pa7']);
            $templateProcessor->setValue('pa8', $info['pa8']);
            $templateProcessor->setValue('pa9', $info['pa9']);
            $templateProcessor->setValue('pa10', $info['pa10']);
            $templateProcessor->setValue('pa11', $info['pa11']);
            $templateProcessor->setValue('pa12', $info['pa12']);
            $templateProcessor->setValue('pa13', $info['pa13']);

            $templateProcessor->setValue('pa15', $info['pa15']);
            $templateProcessor->setValue('pa16', $info['pa16']);
            $templateProcessor->setValue('pa17', $info['pa17']);
            $templateProcessor->setValue('pa18', $info['pa18']);
            $templateProcessor->setValue('pa19', $info['pa19']);
            if(isset($info['list'])){
                $row=count($info['list']);//总行数
                $templateProcessor->cloneRow('num',$row);//复制行
                $pa = $info['list'];
                $pa = array_values($pa);
                for($d=0;$d<$row;$d++){
                    $templateProcessor->setValue("num#".($d+1),$d+1);
                    $templateProcessor->setValue("a1#".($d+1),$pa[$d]['出厂编号']);
                    $templateProcessor->setValue("a2#".($d+1),$pa[$d]['电场名称']);
                    $templateProcessor->setValue("a3#".($d+1),$pa[$d]['一次电压百分比']);
                    $templateProcessor->setValue("a4#".($d+1),$pa[$d]['一次电流百分比']);
                    $templateProcessor->setValue("a5#".($d+1),$pa[$d]['二次电压百分比']);
                    $templateProcessor->setValue("a6#".($d+1),$pa[$d]['二次电流百分比']);
                    $templateProcessor->setValue("a7#".($d+1),$pa[$d]['IP地址']);
                    $templateProcessor->setValue("a8#".($d+1),$pa[$d]['变压器型号']);
                    $templateProcessor->setValue("a9#".($d+1),$pa[$d]['变压器抽头']);
                }
            }
            if(isset($info['list1'])){
                $rows=count($info['list1']);//总行数
                $templateProcessor->cloneRow('nums',$rows);//复制行
                $ba = $info['list1'];
                $ba = array_values($ba);
                for($b=0;$b<$rows;$b++){
                    $templateProcessor->setValue("nums#".($b+1),$b+1);
                    $templateProcessor->setValue("b1#".($b+1),$ba[$b]['输出比']);
                    $templateProcessor->setValue("b2#".($b+1),$ba[$b]['输出频率']);
                    $templateProcessor->setValue("b3#".($b+1),$ba[$b]['一次电压']);
                    $templateProcessor->setValue("b4#".($b+1),$ba[$b]['一次电流']);
                    $templateProcessor->setValue("b5#".($b+1),$ba[$b]['二次电压']);
                    $templateProcessor->setValue("b6#".($b+1),$ba[$b]['二次电压取样电阻']);
                    $templateProcessor->setValue("b7#".($b+1),$ba[$b]['二次电压反馈']);
                    $templateProcessor->setValue("b8#".($b+1),$ba[$b]['二次电流']);
                    $templateProcessor->setValue("b9#".($b+1),$ba[$b]['二次电流取样电阻']);
                }
            }
            unset($info['_file_']);
            unset($info['CURRENT_VALIDATION_STEP']);
            unset($info['ALL_STEPS']);
            unset($info['_token']);
            $info['projectname'] = '智能变频脉冲电源安装调试完成确认单';
            $data['admin_id'] = Admin::user()->id;
            $data['name'] = $info['projectname'].date('Y-m-d H:i');
            $data['data'] = $info;
            // $data['content'] = json_encode($info);
            // $insertData['content'] = json_encode($data);
            $insertData['name'] = $data['name'];
            $insertData['type'] = 8;
            $insertData['admin_id'] = Admin::user()->id;
            $insertData['created_at'] = date('Y-m-d H:i:s');
            $templateProcessor->saveAs(public_path('filemodellist/'.$data['name'].".docx"));
            InformationList::insert($insertData);
            return $this->form()->saving(function (Form $form) {
                // 清空缓存
                $form->multipleSteps()->flushStash();
                // 拦截保存操作
                return response(
                    $form->multipleSteps()
                        ->done()
                        ->render()
                );
            })->store();
        }
    }

    public function list($id,Content $content){
        return $content
            ->header('表格列表')
            ->description()
            ->body($this->grid($id));
    }
    protected function grid($typeid)
    {
        return new Grid(InformationList::where(['admin_id'=>Admin::user()->id,'type'=>$typeid]), function (Grid $grid) {
            $grid->showActions();
            $grid->disableDeleteButton();
            $grid->disableQuickEditButton();
            $grid->disableViewButton();
            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->fixColumns(2);
            
            $grid->column('id', 'ID');
            $grid->column('name', '名称');
            $grid->column('type','类型')->display(function($type) {
                return Db::table('xfy_info_model')->where(['id'=>$type])->value('name');;
            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $id = $actions->getKey();
                $edit_url = admin_base_path('downloadfile/'.$id);
                $actions->append("<a href='$edit_url'>下载</a>");
            });
        });
    }

    /**
     * 保存
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function store(Request $requset)
    {
        return $this->form()->saving(function (Form $form) {
            
            // 清空缓存
            $form->multipleSteps()->flushStash();
            // 拦截保存操作
            return response(
                $form->multipleSteps()
                    ->done()
                    ->render()
            );
        })->store();
    }
}